package bm.com.project.server.organization.service.impl;

import bm.com.framework.common.unit.Tool;
import bm.com.framework.security.util.SecurityUtils;
import bm.com.project.server.common.dto.SearchBaseDTO;
import bm.com.project.server.organization.entity.BmOrgDepartmentEntity;
import bm.com.project.server.organization.mapper.BmOrgDepartmentMapper;
import bm.com.project.server.organization.service.DepartmentService;
import bm.com.framework.common.resp.PageVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DepartmentServiceImpl extends ServiceImpl<BmOrgDepartmentMapper, BmOrgDepartmentEntity> implements DepartmentService {

    @Autowired
    BmOrgDepartmentMapper bmOrgDepartmentDao;

    @Override
    public PageVO<List> getListPage(SearchBaseDTO queue) {
        QueryWrapper<Object> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("orgId",SecurityUtils.getOrgId());
        if (Tool.isStringEmpty(queue.getKeyword())){
            queryWrapper.like("title",queue.getKeyword());
        }

        Integer count = bmOrgDepartmentDao.selectCount(queryWrapper);
        Page<Object> objectPage = new Page<>(queue.getPageNum(),queue.getPageNum());
        IPage<List> bmOrgDepartmentDaoIPage = bmOrgDepartmentDao.selectPage(objectPage, queryWrapper);

        return new PageVO<List>(count, (List) bmOrgDepartmentDaoIPage);
    }

    @Override
    public List<BmOrgDepartmentEntity> getListAll() {
        QueryWrapper<BmOrgDepartmentEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("orgId", SecurityUtils.getOrgId());
        return bmOrgDepartmentDao.selectList(queryWrapper);
    }

    @Override
    public BmOrgDepartmentEntity getDetail(int departmentId) {
        return bmOrgDepartmentDao.selectById(departmentId);
    }
}
